//
// Copyright © 2021 dev. All rights reserved.
// 

import Cocoa
import ConfCore
import Realm
import os.log
@objc final class WWDCAgentService:NSObject {
    let log = OSLog.agentLog(with:String(describing: WWDCAgentService.self))

    private var storage:Storage?
    private var syncEngine:SyncEngine?
    private let boot = Boot()

    lazy var listener:NSXPCListener = {
        let l = NSXPCListener(machServiceName: "io.wwdc.app.WWDCAgent")
        l.delegate = self
        return l
    }()

    func start() {
//        boot.bootstrapDependencies(readOnly: false) { [weak self] result in
//            guard let self = self else { return}
//            switch result {
//            case .success(let dependencies):
//                self.storage = dependencies.storage
//                self.syncEngine = dependencies.syncEngine
//                os_log("Bootstrapped successfully!",log:self.log,type:.default)
//
//            case .failure(_):
//                os_log("Bootstrapped fail!",log:self.log,type:.default)
//
//            }
//        }
        self.startListening();
    }
    private func startListening()  {
        dispatchPrecondition(condition: .onQueue(.main))
        listener.resume()
        os_log("XPC service started",log:self.log,type:.default)
    }

}

extension WWDCAgentService:NSXPCListenerDelegate {
    func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
        os_log("%{public}@ %{public}@",log:log, type:.debug,#function,newConnection)
        guard XPCConnectionValidator.shared.validate(newConnection) else {
            os_log("Refusing connection because the validation failed: %{public}@", log: self.log, type: .error, newConnection)
            return false
        }
        newConnection.exportedInterface = NSXPCInterface(with: WWDCAgentInterface.self)
        newConnection.exportedObject = self
        newConnection.resume()
        return true
    }
}

extension WWDCAgentService:WWDCAgentInterface {
    func fetchFavoriteSessions(for event: String?, completion: @escaping ([String]) -> Void) {

    }

    func fetchDownloadedSessions(for event: String?, completion: @escaping ([String]) -> Void) {

    }

    func fetchWatchedSessions(for event: String?, completion: @escaping ([String]) -> Void) {

    }

    func fetchUnwatchedSessions(for event: String?, completion: @escaping ([String]) -> Void) {

    }

    func revealVideo(with id: String, completion: @escaping (Bool) -> Void) {

    }

    func setFavorite(_ isFavorite: Bool, for videoId: String, completion: @escaping (Bool) -> Void) {

    }

    func setWatched(_ watched: Bool, for videoId: String, completion: @escaping (Bool) -> Void) {

    }

    func startDownload(for videoId: String, completion: @escaping (Bool) -> Void) {

    }

    func stopDownload(for videoId: String, completion: @escaping (Bool) -> Void) {
            
    }

        func testAgentConnection(with completion: @escaping (Bool) -> Void){
            os_log("%{public}@",log:log, type:.debug,#function)
            completion(true)
        }
    }
